package com.ckerchina.admin.ckadmin.controller.console;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ckerchina.admin.ckadmin.controller.dto.MgrResponseData;
import com.ckerchina.admin.ckadmin.dao.ChargeDetailDao;
import com.ckerchina.admin.ckadmin.domain.ChargeDetail;
import com.ckerchina.admin.ckadmin.domain.mgr.MgrUser;
import com.ckerchina.admin.ckadmin.service.UpdateService;
import com.ckerchina.admin.ckadmin.utils.Page;

@Controller
public class ChargeDetailController extends AbstractConsoleController {

	@Autowired
	private ChargeDetailDao chargeDetailDao;

	@RequestMapping("chargedetail/list")
	public @ResponseBody Page<ChargeDetail> list(String agent_id, int start, int limit) {
		String where = "";
		Map<String, Object> params = new HashMap<String, Object>();
		// if (!"".equals(agent_id)) {
		// where += " and agent_id = :agent_id";
		// params.put("agent_id", agent_id);
		// }

		MgrUser user = getMgrUser();
		if (user.getActortype().equals("1"))// 代理商
		{
			where += " and agent_id = :agent_id";
			params.put("agent_id", Long.parseLong(user.getCompanyid()));
		} else if (!"".equals(agent_id)) {
			where += " and agent_id = :agent_id";
			params.put("agent_id", agent_id);
		}

		return chargeDetailDao.findPage(where, params, start, limit, " order by id desc");
	}

	@RequestMapping("chargedetail/list_uncheck")
	public @ResponseBody Page<ChargeDetail> list_uncheck(String agent_id, int start, int limit) {
		String where = "";
		Map<String, Object> params = new HashMap<String, Object>();
		if (!"".equals(agent_id)) {
			where += " and agent_id = :agent_id";
			params.put("agent_id", agent_id);
		}

		where += " and state = :state";
		params.put("state", 1);

		return chargeDetailDao.findPage(where, params, start, limit, " order by id desc");
	}

	@Autowired
	UpdateService updateService;

	@RequestMapping("chargedetail/add")
	public @ResponseBody MgrResponseData add(ChargeDetail charge) {
		updateService.addChargeDetail(charge);
		// updateService.addAgentUser(finfo);
		return new MgrResponseData("充值成功");
	}

	@RequestMapping("chargedetail/deduct")
	public @ResponseBody MgrResponseData deduct(ChargeDetail charge) {
		updateService.deductChargeDetail(charge);
		// updateService.addAgentUser(finfo);
		return new MgrResponseData("扣款成功");
	}

	@RequestMapping("chargedetail/update")
	public @ResponseBody MgrResponseData update(ChargeDetail charge) {
		updateService.updateChargeDetail(charge);
		// updateService.updateAgentUser(finfo);
		return new MgrResponseData("修改成功");
	}
}
